package com.polaris.live.utils.image

import android.graphics.drawable.Drawable
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.GranularRoundedCorners
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.polaris.live.R

/**
 * glide相关
 *
 * @author Created by lucas on 2023/10/23 14:09
 */

/**
 * 加载图片
 */
fun RequestManager.loadGiftImage(url: String?): RequestBuilder<Drawable> {
    return load(ImageUtils.imgPath(url))
        .normalGift()
}

/**
 * 添加异常和正常的礼物图片
 */
fun RequestBuilder<Drawable>.normalGift(): RequestBuilder<Drawable> {
    return placeholder(R.drawable.icon_gift_empty)
        .error(R.drawable.icon_gift_empty)
}

/**
 * 加载图片
 */
fun RequestManager.loadImage(url: String?): RequestBuilder<Drawable> {
    return load(ImageUtils.imgPath(url))
        .normal()
}

private val wejoyLoading = PlaceholderDrawable(R.drawable.salalogo, 0.6f, 0xececec)

/**
 * 加载图片(wejoy)背景
 */
fun RequestManager.loadWejoyImage(url: String?, radius: Int? = null): RequestBuilder<Drawable> {
    return load(ImageUtils.imgPath(url))
        .placeholder(wejoyLoading)
        .error(wejoyLoading)
}

/**
 * 加载头像
 */
fun RequestManager.loadAvatar(url: String?): RequestBuilder<Drawable> {
    return load(ImageUtils.imgPathSmall(url))
        .circleCrop()
        .avatar()
}

/**
 * 添加异常和正常的图片
 */
fun RequestBuilder<Drawable>.normal(): RequestBuilder<Drawable> {
    return placeholder(R.color.gray99)
        .error(R.color.gray99)
}

/**
 * 添加异常和正常的头像图片
 */
fun RequestBuilder<Drawable>.avatar(): RequestBuilder<Drawable> {
    return placeholder(R.drawable.avatar_default)
        .error(R.drawable.avatar_default)
}

/**
 * 变换圆角
 */
fun RequestBuilder<Drawable>.transformRounded(roundingRadius: Int): RequestBuilder<Drawable> {
    return transform(CenterCrop(), RoundedCorners(roundingRadius)).normal()
}

/**
 * 变换圆角
 */
fun RequestBuilder<Drawable>.transformRounded(
    topLeft: Int,
    topRight: Int,
    bottomLeft: Int,
    bottomRight: Int
): RequestBuilder<Drawable> {
    return normal()
        .transform(
            CenterCrop(),
            GranularRoundedCorners(
                topLeft.toFloat(),
                topRight.toFloat(),
                bottomRight.toFloat(),
                bottomLeft.toFloat()
            )
        )
}